package com.dorukdestan.p2pshare.model;

public class Protocol {
	
	public static enum ProtocolCommand{
		/**
		 * PING
		 */
		PING,
		
		/**
		 * PONG
		 */
		PONG,
		
		/**
		 * SRCH [mode] [fileName] <br>
		 * SRCH [mode] [fileHash]
		 */
		SEARCH,
		
		/**
		 * NONE
		 */
		NONE,
		
		/**
		 * FILE [fileHash] [fileLength] [fileName]
		 */
		FILE_FOUND,
		
		/**
		 * GETF [fileHash] [fileIndexStart] [fileIdexEnd]
		 */
		DOWNLOAD_FILE,
		
		/**
		 * SNDF [fileHash] [fileIndexStart] [data]
		 */
		SENDING_FILE;
	}

	public static final String DELIMINATOR_CHAR = " ";
	
	/**
	 * PING
	 */
	public static final String PING = "PING";
	
	/**
	 * PONG
	 */
	public static final String PONG = "PONG";
	
	/**
	 * SRCH
	 */
	public static final String SEARCH = "SRCH";
	
	/**
	 * NONE
	 */
	public static final String NONE = "NONE";
	
	/**
	 * FOND
	 */
	public static final String FILE_FOUND = "FOND";
	
	/**
	 * GETF
	 */
	public static final String DOWNLOAD_FILE = "GETF";
	
	/**
	 * SNDF
	 */
	public static final String SENDING_FILE = "SNDF";
	
	public static final String SEARCH_MODE_NAME = "NAME";
	public static final String SEARCH_MODE_HASH = "HASH";
	
	public static ProtocolCommand getProtocol(String receivedStr){
		String[] res = receivedStr.split(DELIMINATOR_CHAR);
		
		if(res.length < 1)
			return ProtocolCommand.NONE;
		
		String command = res[0];
		if(command.equalsIgnoreCase(PING))
			return ProtocolCommand.PING;
		if(command.equalsIgnoreCase(PONG))
			return ProtocolCommand.PONG;
		if(command.equalsIgnoreCase(SEARCH))
			return ProtocolCommand.SEARCH;
		if(command.equalsIgnoreCase(NONE))
			return ProtocolCommand.NONE;
		if(command.equalsIgnoreCase(FILE_FOUND))
			return ProtocolCommand.FILE_FOUND;
		if(command.equalsIgnoreCase(DOWNLOAD_FILE))
			return ProtocolCommand.DOWNLOAD_FILE;
		if(command.equalsIgnoreCase(SENDING_FILE))
			return ProtocolCommand.SENDING_FILE;
		
		return ProtocolCommand.NONE;
	}
	/**
	 * @param receivedStr
	 * @return returns an array of arguments. Note that protocol command is omitted.
	 */
	public static String[] getProtocolArguments(String receivedStr){
		String[] res = receivedStr.split(DELIMINATOR_CHAR);
		String [] args = new String[res.length-1];
		System.arraycopy(res, 1, args, 0, res.length-1);
		
		return args;
	}
}
